package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServicePriceList;
import com.eastfair.venueservice.service.ServicePriceListService;
import com.eastfair.venueservice.vo.ServicePriceListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 价目表
 * </p>
 *
 * @author linan
 * @date 2022-07-21
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/servicePriceList")
@Api(value = "ServicePriceList", tags = "价目表")
@PreAuth(replace = "venueservice:servicePriceList:")
public class ServicePriceListController extends SuperController<ServicePriceListService, Long, ServicePriceList, ServicePriceListPageQuery, ServicePriceListSaveDTO, ServicePriceListUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServicePriceList> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServicePriceList> servicePriceListList = list.stream().map((map) -> {
            ServicePriceList servicePriceList = ServicePriceList.builder().build();
            //TODO 请在这里完成转换
            return servicePriceList;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(servicePriceListList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServicePriceListPageQuery> params) {
        ServicePriceListPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 添加价目
     *
     * @param servicePriceList 添加价目
     * @return R
     */
    @ApiOperation(value = "添加价目", notes = "添加价目")
    @SysLog("添加价目")
    @PostMapping("/addPriceList")
    public R<Boolean> addPriceList(@RequestBody  @Validated(ServicePriceListDTO.Save.class) ServicePriceListDTO servicePriceList) {
        log.info("addPriceList - 添加服务 servicePriceList={}", servicePriceList);
        try {
            return baseService.addPriceList(servicePriceList);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 更新价目
     *
     * @param servicePriceList 更新价目
     * @return R
     */
    @ApiOperation(value = "更新价目", notes = "更新价目")
    @SysLog("修改价目")
    @PostMapping("/updatePriceList")
    public R<Boolean> updatePriceList(@RequestBody  @Validated(ServicePriceListDTO.Update.class) ServicePriceListDTO servicePriceList) {
        log.info("updatePriceList - 更新价目 servicePriceList={}", servicePriceList);
        try {
            return baseService.updatePriceList(servicePriceList);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 查询价目
     *
     * @param servicePriceList 查询价目
     * @return R
     */
    @ApiOperation(value = "查询价目", notes = "查询价目")
    @PostMapping("/queryPriceList")
    public R<IPage<ServicePriceListVO>> queryPriceList(@RequestBody PageParams<ServicePriceListQuery> servicePriceList) {
        log.info("queryPriceList - 查询价目 servicePriceList={}", servicePriceList);
        return baseService.queryPriceList(servicePriceList);
    }

    /**
     * 批量删除价目
     *
     * @param servicePriceList 批量删除价目
     * @return R
     */
    @ApiOperation(value = "批量删除价目", notes = "批量删除价目")
    @SysLog("删除价目")
    @PostMapping("/delPriceListBatch")
    public R<Boolean> delPriceListBatch(@RequestBody @Validated(ServicePriceListDTO.Primary.class) @NotEmpty(message = "参数不能为空") List<ServicePriceListDTO> servicePriceList) {
        log.info("delPriceList - 批量删除价目 delPriceListBatch={}", servicePriceList);
        return R.success(baseService.removeByIdsOfLogic(ConvertUtil.convertList(servicePriceList, ServicePriceList.class)));
    }

    /**
     * 删除价目
     *
     * @param servicePriceList 删除价目
     * @return R
     */
    @ApiOperation(value = "删除价目", notes = "删除价目")
    @SysLog("删除价目")
    @PostMapping("/delPriceList")
    public R<Boolean> delPriceList(@RequestBody @Validated(ServicePriceListDTO.Primary.class) ServicePriceListDTO servicePriceList) {
        log.info("delPriceList - 删除价目 servicePriceList={}", servicePriceList);
        return R.success(baseService.delPriceList(servicePriceList));
    }

    /**
     * 复制价目
     *
     * @param servicePriceList 复制价目
     * @return R
     */
    @ApiOperation(value = "复制价目", notes = "复制价目")
    @SysLog("复制价目")
    @PostMapping("/copyPriceList")
    public R<Boolean> copyPriceList(@RequestBody @Validated(ServicePriceListDTO.Primary.class) ServicePriceListDTO servicePriceList) {
        log.info("copyPriceList - 复制价目 servicePriceList={}", servicePriceList);
        return R.success(baseService.copyPriceList(servicePriceList));
    }

}
